//
//  HNLoginViewController.m
//  HappyNews
//
//  Created by baiping on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HNLoginViewController.h"
#import "UMSocial.h"
@interface HNLoginViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) HNLoginView *loginView;


@property (nonatomic, strong) UIButton *longBtn;

@end

@implementation HNLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLoginView];
    
//    _longBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    
//    _longBtn.frame = CGRectMake(50, 50, 50, 50);
//    
    
    
//    [self.view addSubview:_longBtn];
//    
//    _longBtn.backgroundColor = [UIColor redColor];
//    
//    [_longBtn addTarget:self action:@selector(sinaLoginAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    
    
}

- (void)addLoginView{

    
    _loginView = [[UINib nibWithNibName:@"LoginView" bundle:nil]instantiateWithOwner:self options:nil].firstObject;
    
    _loginView.frame = self.view.bounds;
    
    [self.view addSubview:_loginView];
    
    [_loginView.backButton  setBackgroundImage:[UIImage imageNamed:@"Back"] forState:(UIControlStateNormal)];
    
    _loginView.passWordTextFeild.delegate = self;
    
    _loginView.nameTextFeid.delegate = self;
    
    // 给返回按钮添加方法
    [_loginView.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
#pragma --mark  实现新浪微博登陆方法
    
    [_loginView.sinaLogin setBackgroundImage:[UIImage imageNamed:@"Comment_Share_Sina"] forState:(UIControlStateNormal)];
    
    [_loginView.sinaLogin addTarget:self action:@selector(sinaLoginAction:) forControlEvents:(UIControlEventTouchUpInside)];
 
    
    
}

// 实现新浪微博登陆地方法
- (void)sinaLoginAction:(UIButton *)sender{
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            
            [defaults setObject:snsAccount.userName forKey:@"name"];
            
            [defaults setObject:snsAccount.iconURL forKey:@"iconURL"];
            
            if (_delegate && [_delegate respondsToSelector:@selector(loginViewControllerDidFinishLogin)]) {
                
                [_delegate loginViewControllerDidFinishLogin];
                
                
            }else{
                
                
                [MBProgressHUD showError:@"登录失败"];
                
                return ;
                
            }
        
        }});
    
    // 先暂时写在这里之后改效果 用户页登录新浪界面跳出后由bug 但是setting界面没有
#warning
    [self dismissViewControllerAnimated:NO completion:nil];

    [self.navigationController popViewControllerAnimated:NO];
    
    
}

// 实现返回按钮的方法
- (void)backButtonAction:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_loginView endEditing:YES];
    
    
    return YES;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_loginView endEditing:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
